package com.lminkey.servlet;

import com.lmonkey.dao.Basedao;
import com.lmonkey.servic.USERDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet("/shangc/usernamecheck")
public class UsernameCheck extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        //获取参数
        String name = request.getParameter("name");
        String name1 = request.getParameter("id");

        int count = 0;
        Connection conn = Basedao.getconn();
        PreparedStatement ps = null;
        ResultSet rs = null;
        try{
            String sql = null;
            if (name1 == null){
                sql = "select count(*) from lmonkeyshop where user_id = ?";
                ps = conn.prepareStatement(sql);
                ps.setObject(1,name);
            }else {
                sql = "select count(*) from lmonkeyshop where user_id = ? and user_id != ?";
                ps = conn.prepareStatement(sql);
                ps.setObject(1,name);
                ps.setObject(2,name1);
            }
            rs = ps.executeQuery();
            while(rs.next()){
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        PrintWriter out = response.getWriter();
        if (count>0){
            out.print("false");
        }else{
            out.print("true");
        }
        out.close();
    }
}
